import 'package:flu_framework/common/widgets/double_tap_back_exit_app.dart';
import 'package:flu_framework/ui/home/pages/home_page.dart';
import 'package:flu_framework/ui/mine/pages/mine_page.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class Toolbar extends StatefulWidget {
  Toolbar({Key? key}) : super(key: key);

  @override
  State<Toolbar> createState() => _ToolbarState();
}

class _ToolbarState extends State<Toolbar> {
  int cuttentIndex = 0;
  DateTime? lastPopTime;
  PageController _pageController = PageController();

  final List<Image> _tabImages = [
    Image(image: AssetImage('images/course_table_tabBar_unselected.png'),gaplessPlayback: true,),
    Image(image: AssetImage('images/mine_tabBar_unselected.png'),gaplessPlayback: true,),
  ];
  final List<Image> _tabSelectedImages = [
    Image(image: AssetImage('images/course_table_tabBar_selected.png'),gaplessPlayback: true,),
    Image(image: AssetImage('images/mine_tabBar_selected.png'),gaplessPlayback: true,)
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:DoubleTapBackExitApp(
        child: PageView(
          controller: _pageController,
          physics: NeverScrollableScrollPhysics(),
          children: const [
            HomePage(),
            MinePage(),
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        selectedItemColor: Color.fromRGBO(43, 76, 126, 1),
        unselectedItemColor: Color.fromRGBO(43, 76, 126, .5),
        currentIndex: cuttentIndex,
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          setState(() {
            _pageController.jumpToPage(index);
            cuttentIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: getTabIcon(0),
            label: '首页',
          ),
          BottomNavigationBarItem(
            icon: getTabIcon(1),
            label: '我的',
          ),
        ],
      ),
    );
  }

  Image getTabIcon(int index) {
    if (index == cuttentIndex) {
      return _tabSelectedImages[index];
    } else {
      return _tabImages[index];
    }
  }
}
